//
//  XHRTopicCell.m
//  BuDeJie
//
//  Created by haoran on 16/4/13.
//  Copyright © 2016年 xuhaoran. All rights reserved.
//

#import "XHRTopicCell.h"
#import "XHRTopicItem.h"
#import <SDWebImage/UIImageView+WebCache.h>

#import "XHRTopicVideoView.h"
#import "XHRTopicVoiceView.h"
#import "XHRTopicPictureView.h"

@interface  XHRTopicCell ()

@property (weak, nonatomic) IBOutlet UIImageView *profileImageView;

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;

@property (weak, nonatomic) IBOutlet UILabel *pastTimeLabel;

@property (weak, nonatomic) IBOutlet UILabel *text_Label;

@property (weak, nonatomic) IBOutlet UIButton *dingButton;

@property (weak, nonatomic) IBOutlet UIButton *caiButton;

@property (weak, nonatomic) IBOutlet UIButton *repostButton;

@property (weak, nonatomic) IBOutlet UIButton *commentButton;

@property (weak, nonatomic) IBOutlet UIView *topCmtView;

@property (weak, nonatomic) IBOutlet UILabel *topCmtLabel;

/**  中间控件  **/
/** 中间图片控件 **/
@property (nonatomic, weak)  XHRTopicPictureView *pictureView;
/** 中间视频控件 **/
@property (nonatomic, weak)  XHRTopicVideoView *videoView;
/** 中间音频控件 **/
@property (nonatomic, weak)  XHRTopicVoiceView *voiceView;

@end
@implementation XHRTopicCell
#pragma mark - 初始化
- (void)awakeFromNib {
  
    // 设置背景图片
    self.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"mainCellBackground"]];
}

-(void)setFrame:(CGRect)frame{
    
    frame.size.height -= XHRMargin;
    [super setFrame:frame];
}


#pragma mark - 懒加载
-(XHRTopicPictureView *)pictureView{
    if (_pictureView == nil) {
        _pictureView = [XHRTopicPictureView xhr_viewFromXib];
        // 添加到当前View
        [self.contentView addSubview:_pictureView];
    }

    return _pictureView;
}

-(XHRTopicVideoView *)videoView{
    if (_videoView == nil) {
        _videoView = [XHRTopicVideoView xhr_viewFromXib];
        // 添加到当前View
        [self.contentView addSubview:_videoView];
    }
    
    return _videoView;
}

-(XHRTopicVoiceView *)voiceView{
    if (_voiceView == nil) {
        _voiceView = [XHRTopicVoiceView xhr_viewFromXib];
        // 添加到当前View
        [self.contentView addSubview:_voiceView];
    }
    
    return _voiceView;
}

#pragma mark - 模型set方法
/**
 *  topicItem模型set方法
 */
-(void)setTopicItem:(XHRTopicItem *)topicItem{
    
    _topicItem = topicItem;
    
    // 设置头像,昵称等
    [self.profileImageView sd_setImageWithURL:[NSURL URLWithString:topicItem.profile_image] placeholderImage:[UIImage imageNamed:@"defaultUserIcon"]];
    self.nameLabel.text = topicItem.name;
    self.pastTimeLabel.text = topicItem.passtime;
    self.text_Label.text = topicItem.text;
    
    // 底部按钮
    [self setButton:self.dingButton number:topicItem.ding placeHolder:@"顶"];
    [self setButton:self.caiButton number:topicItem.cai placeHolder:@"踩"];
    [self setButton:self.repostButton number:topicItem.repost placeHolder:@"分享"];
    [self setButton:self.commentButton number:topicItem.comment placeHolder:@"评论"];
    
    // 最热评论
    if (topicItem.top_cmt.count) {
        
        self.topCmtView.hidden = NO;
        
        NSString * userName = self.topicItem.top_cmt.firstObject[@"user"][@"username"];
        NSString * content = self.topicItem.top_cmt.firstObject[@"content"];
        
        if (content.length == 0) { //语音评论
            content = @"语音评论";
        }
        self.topCmtLabel.text = [NSString stringWithFormat:@"%@ : %@",userName,content];
        
    }else {
    
        self.topCmtView.hidden = YES;
    }
    
    // 添加中间的内容
    switch (topicItem.type) {
        case XHRTopicTypePicture:{ // 循环利用,需处理
            self.voiceView.hidden = YES;
            self.videoView.hidden = YES;
            self.pictureView.hidden = NO;
            self.pictureView.topicItem = topicItem;
            break;
        }
        
        case XHRTopicTypeVideo:{
            self.videoView.hidden = NO;
            self.videoView.topicItem = self.topicItem;
            self.pictureView.hidden = YES;
            self.voiceView.hidden = YES;
            break;
        }
            
        case XHRTopicTypeVoice:{
            self.voiceView.hidden = NO;
            self.voiceView.topicItem = self.topicItem;
            self.pictureView.hidden = YES;
            self.videoView.hidden = YES;
            break;
        }
            
        case XHRTopicTypeWord:{
            self.voiceView.hidden = YES;
            self.pictureView.hidden = YES;
            self.videoView.hidden = YES;
        }
        default:
            break;
    }
    
    
}

- (void)setButton:(UIButton *)button number:(NSInteger)number placeHolder:(NSString *)placeHolder{
    
    if (number >= 10000) {
        [button setTitle:[NSString stringWithFormat:@"%.1f万",number / 10000.0]forState:UIControlStateNormal];
    } else if (number > 0 ){
        [button setTitle:[NSString stringWithFormat:@"%zd",number] forState:UIControlStateNormal];
    }else {
        [button setTitle:placeHolder forState:UIControlStateNormal];
    }
}

#pragma mark - 子控件布局
-(void)layoutSubviews{
    
    [super layoutSubviews];

    switch (self.topicItem.type) {
        case XHRTopicTypePicture:{
            self.pictureView.frame = self.topicItem.middleFrame;
            break;
        }
            
        case XHRTopicTypeVideo:{
            self.videoView.frame = self.topicItem.middleFrame;
            break;
        }
            
        case XHRTopicTypeVoice:{
            self.voiceView.frame = self.topicItem.middleFrame;
            break;
        }
        default:
            break;
    }
    
}

@end
